/*ZUQT01BjvxtEUKEI5EE59UNnZIvG9tWMAQey/ef4+YsRjlqt2V8F+8m65cz3bplv*/
import { equipTransferPageCode, equipAppcode, equipTransferHead, equipTransferSearch } from '../constants';
import { linkCard } from './buttonClick';

import ampub from 'ampub';

const { components, commonConst } = ampub;
const { LoginContext, refInit, assetOrgMultiRefFilter } = components;
const { getContext, loginContextKeys } = LoginContext;
const { defRefCondition, commonRefCondition } = refInit;
const { AssetOrgMultiRefFilter } = assetOrgMultiRefFilter;
const { CommonKeys } = commonConst;
const { IBusiRoleConst } = CommonKeys;

export default function(props) {
	props.createUIDom(
		{
			pagecode: equipTransferPageCode,
			appcode: equipAppcode
		},
		(data) => {
			if (data) {
				if (data.template) {
					let meta = data.template;
					meta = modifierMeta.call(this, props, meta);
					props.meta.addMeta(meta);
				}
			}
		}
	);
}

function modifierMeta(props, meta) {
	// modified by zhangypm 2020年3月2日
	// 直接复制 设备卡片得参照过滤代码（由于固定资产公共方法中都是财务组织，而设备卡片为资产组织）
	const pk_group = getContext(loginContextKeys.pk_group);
	meta[equipTransferSearch].items.map((item) => {
		//货主管理组织
		if (item.attrcode == 'pk_ownerorg' || item.attrcode == 'pk_usedorg') {
			item.queryCondition = () => {
				return { GridRefActionExt: 'nccloud.web.ampub.common.refCodition.AppPermissionOrgRefFilter' };
			};
		} else if (item.attrcode == 'pk_mandept') {
			//管理部门
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_ownerorg');
			getQueryCondition.call(this, props, item);
			item.isRunWithChildren = true;
			item.defaultRunWithChildren = true;
		} else if (item.attrcode == 'pk_manager') {
			//管理人
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_ownerorg');
			getQueryCondition.call(this, props, item);
		} else if (item.attrcode == 'pk_usedept') {
			//使用部门
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_usedunit');
			getQueryCondition.call(this, props, item, 'pk_usedunit');
			item.isRunWithChildren = true;
			item.defaultRunWithChildren = true;
		} else if (item.attrcode == 'pk_user') {
			//使用人
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_usedunit');
			getQueryCondition.call(this, props, item, 'pk_usedunit');
		} else if (item.attrcode == 'pk_location') {
			//位置
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_ownerorg');
			getQueryCondition.call(this, props, item);
		} else if (item.attrcode == 'pk_supplier') {
			//供应商
			AssetOrgMultiRefFilter.call(this, props, equipTransferSearch, item, 'pk_ownerorg');
			getQueryCondition.call(this, props, item);
		} else if (item.attrcode == 'pk_specialty') {
			//专业
			item.queryCondition = () => {
				let pk_org = getSearchValue.call(this, props, 'pk_ownerorg');
				return { pk_org };
			};
		} else {
			let bodyDefPrefix = 'bodyvos.def';
			defRefCondition.call(this, props, item, equipTransferSearch, pk_group, true, 'pk_org', bodyDefPrefix);
			defRefCondition.call(this, props, item, equipTransferSearch, pk_group, true);
		}
		commonRefCondition.call(this, props, item);
	});
	

	//增加超链接
	meta[equipTransferHead].items = meta[equipTransferHead].items.map((item, key) => {
		if (item.attrcode == 'equip_code') {
			item.render = (text, record, index) => {
				let transType = record.transi_type.value;
				return (
					<div class="simple-table-td">
						<span
							className="code-detail-link"
							onClick={() => {
								linkCard.call(this, props, record, 'pk_equip', transType);
							}}
						>
							{record && record.equip_code && record.equip_code.value}
						</span>
					</div>
				);
			};
		}

		return item;
	});
	return meta;
}

//获取查询条件的值
function getSearchValue(props, field) {
	let data = props.search.getSearchValByField(equipTransferSearch, field);
	if (data && data.value && data.value.firstvalue && 20 == data.value.firstvalue.length) {
		return data.value.firstvalue;
	}
}

/**
 * 获取字段的过滤条件，针对是部门和人员等需要组织多选参照的字段
 */
function getQueryCondition(props, item, key = 'pk_ownerorg') {
	item.queryCondition = () => {
		let pk_org = getSearchValue.call(this, props, key); //主组织
		let filter = { busifuncode: IBusiRoleConst.ASSETORG };
		if (pk_org) {
			filter['pk_org'] = pk_org;
		}
		return filter; // 根据pk_org过滤
	};
}


/*ZUQT01BjvxtEUKEI5EE59UNnZIvG9tWMAQey/ef4+YsRjlqt2V8F+8m65cz3bplv*/